package cn.ym.mall.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter("*.do")
public class ExceptionFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("异常处理过滤器启动");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        try {
            System.out.println("异常过滤器");
            filterChain.doFilter(servletRequest, servletResponse);
        } catch (Exception e) {
            servletRequest.setAttribute("errorInfo",e.getMessage());
            servletRequest.getRequestDispatcher("/404.jsp").forward(servletRequest, servletResponse);
        }
    }

    @Override
    public void destroy() {
        System.out.println("异常处理过滤器销毁");
    }
}
